package com.weyoung.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.weyoung.app.dto.req.StatsQueryDTO;
import com.weyoung.app.dto.resp.StatsResultDTO;
import com.weyoung.app.mapper.AccessStatsMapper;
import com.weyoung.app.mapper.DeviceStatsMapper;
import com.weyoung.app.mapper.NetworkStatsMapper;
import com.weyoung.app.service.StatsService;
import com.weyoung.entity.AccessStats;
import com.weyoung.entity.DeviceStats;
import com.weyoung.entity.NetworkStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 统计服务实现类
 * 实现各种统计数据的查询和处理逻辑
 */
@Service
public class StatsServiceImpl implements StatsService {

    @Autowired
    private AccessStatsMapper accessStatsMapper;

    @Autowired
    private NetworkStatsMapper networkStatsMapper;

    @Autowired
    private DeviceStatsMapper deviceStatsMapper;

    /**
     * 日期格式化工具
     */
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public List<StatsResultDTO> getAccessStats(StatsQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<AccessStats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDTO.getUserId() != null, AccessStats::getUserId, queryDTO.getUserId())
                .eq(queryDTO.getUrl() != null, AccessStats::getUrl, queryDTO.getUrl())
                .ge(queryDTO.getStartDate() != null, AccessStats::getDate, queryDTO.getStartDate())
                .le(queryDTO.getEndDate() != null, AccessStats::getDate, queryDTO.getEndDate())
                .orderByAsc(AccessStats::getDate);

        // 查询数据并转换为DTO
        List<AccessStats> statsList = accessStatsMapper.selectList(wrapper);
        return statsList.stream().map(stats -> {
            StatsResultDTO dto = new StatsResultDTO();
            dto.setDateStr(DATE_FORMAT.format(stats.getDate()))
                    .setPv(stats.getPv().longValue())
                    .setUv(stats.getUv().longValue())
                    .setUip(stats.getUip().longValue());
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<StatsResultDTO> getNetworkStats(StatsQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<NetworkStats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDTO.getUserId() != null, NetworkStats::getUserId, queryDTO.getUserId())
                .eq(queryDTO.getUrl() != null, NetworkStats::getUrl, queryDTO.getUrl())
                .ge(queryDTO.getStartDate() != null, NetworkStats::getDate, queryDTO.getStartDate())
                .le(queryDTO.getEndDate() != null, NetworkStats::getDate, queryDTO.getEndDate());

        // 查询数据
        List<NetworkStats> statsList = networkStatsMapper.selectList(wrapper);
        
        // 按网络类型分组统计
        Map<String, Long> networkCounts = statsList.stream()
                .collect(Collectors.groupingBy(NetworkStats::getNetwork, Collectors.counting()));

        // 转换为DTO列表
        return networkCounts.entrySet().stream().map(entry -> {
            StatsResultDTO dto = new StatsResultDTO();
            dto.setDimension(entry.getKey())
                    .setCount(entry.getValue());
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public List<StatsResultDTO> getDeviceStats(StatsQueryDTO queryDTO) {
        // 构建查询条件
        LambdaQueryWrapper<DeviceStats> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(queryDTO.getUserId() != null, DeviceStats::getUserId, queryDTO.getUserId())
                .eq(queryDTO.getUrl() != null, DeviceStats::getUrl, queryDTO.getUrl())
                .ge(queryDTO.getStartDate() != null, DeviceStats::getDate, queryDTO.getStartDate())
                .le(queryDTO.getEndDate() != null, DeviceStats::getDate, queryDTO.getEndDate());

        // 查询数据
        List<DeviceStats> statsList = deviceStatsMapper.selectList(wrapper);
        
        // 按设备类型分组统计
        Map<String, Long> deviceCounts = statsList.stream()
                .collect(Collectors.groupingBy(DeviceStats::getDevice, Collectors.counting()));

        // 转换为DTO列表
        return deviceCounts.entrySet().stream().map(entry -> {
            StatsResultDTO dto = new StatsResultDTO();
            dto.setDimension(entry.getKey())
                    .setCount(entry.getValue());
            return dto;
        }).collect(Collectors.toList());
    }
} 